-- This script was generated by the Schema Diff utility in pgAdmin 4
-- For the circular dependencies, the order in which Schema Diff writes the objects is not very sophisticated
-- and may require manual changes to the script to ensure changes are applied in the correct order.
-- Please report an issue for any failure with the reproduction steps.
CREATE
OR REPLACE VIEW public.request_cache_rbac AS
SELECT
  r.id,
  r.created_at,
  r.body,
  r.path,
  r.auth_hash,
  r.user_id,
  r.prompt_id,
  r.properties,
  r.formatted_prompt_id,
  r.prompt_values,
  ch.created_at AS cached_created_at
FROM
  request r
  JOIN user_api_keys u ON r.auth_hash = u.api_key_hash
  LEFT JOIN cache_hits ch ON ch.request_id = r.id
WHERE
  auth.uid() = u.user_id
UNION
SELECT
  r.id,
  r.created_at,
  r.body,
  r.path,
  r.auth_hash,
  r.user_id,
  r.prompt_id,
  r.properties,
  r.formatted_prompt_id,
  r.prompt_values,
  NULL :: timestamp with time zone AS cached_created_at
FROM
  request r
  JOIN user_api_keys u ON r.auth_hash = u.api_key_hash
WHERE
  auth.uid() = u.user_id;

ALTER TABLE
  public.request_cache_rbac OWNER TO postgres;

GRANT ALL ON TABLE public.request_cache_rbac TO authenticated;

GRANT ALL ON TABLE public.request_cache_rbac TO postgres;

GRANT ALL ON TABLE public.request_cache_rbac TO anon;

GRANT ALL ON TABLE public.request_cache_rbac TO service_role;

-- Changing the columns in a view requires dropping and re-creating the view.
-- This may fail if other objects are dependent upon this view,
-- or may cause procedural functions to fail if they are not modified to
-- take account of the changes.
DROP VIEW public.response_and_request_rbac;

CREATE
OR REPLACE VIEW public.response_and_request_rbac AS
SELECT
  response.body AS response_body,
  response.id AS response_id,
  CASE
    WHEN request.cached_created_at IS NOT NULL THEN request.cached_created_at
    ELSE response.created_at
  END AS response_created_at,
  request.id AS request_id,
  request.body AS request_body,
  request.path AS request_path,
  CASE
    WHEN request.cached_created_at IS NOT NULL THEN request.cached_created_at
    ELSE request.created_at
  END AS request_created_at,
  request.user_id AS request_user_id,
  user_api_keys.api_key_preview,
  user_api_keys.user_id,
  request.properties AS request_properties,
  request.formatted_prompt_id,
  request.prompt_values,
  prompt.name AS prompt_name,
  prompt.prompt AS prompt_regex,
  request.cached_created_at IS NOT NULL AS is_cached
FROM
  response
  LEFT JOIN request_cache_rbac request ON request.id = response.request
  LEFT JOIN user_api_keys ON user_api_keys.api_key_hash = request.auth_hash
  LEFT JOIN prompt ON request.formatted_prompt_id = prompt.id
WHERE
  auth.uid() = user_api_keys.user_id;

GRANT ALL ON TABLE public.response_and_request_rbac TO authenticated;

GRANT ALL ON TABLE public.response_and_request_rbac TO postgres;

GRANT ALL ON TABLE public.response_and_request_rbac TO anon;

GRANT ALL ON TABLE public.response_and_request_rbac TO service_role;